package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;

public class UserMenuPortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/user_menu_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		PortletPreferences preferences = request.getPreferences();	
		//TODO: get user abilities list and save them in preferences
		ArrayList<String> userMenuItemsList = new ArrayList<String>();
		
		userMenuItemsList.add(PortletsUtils.USER_PROJECTS_PAGE);
		userMenuItemsList.add(PortletsUtils.USER_TASKS_PAGE);
		userMenuItemsList.add(PortletsUtils.USER_MESSAGES_PAGE);		
			
		preferences.setValues(PortletsUtils.USER_MENU_ITEMS, userMenuItemsList.toArray(new String[0]));		
		
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {		
		String value = request.getParameter(PortletsUtils.USER_MENU_KEY);		
		if (value.equals(PortletsUtils.USER_PROJECTS_PAGE)) {			
			response.sendRedirect("/portal/portal/user_main_page.psml");
		} else if (value.equals(PortletsUtils.USER_TASKS_PAGE)) {
			response.sendRedirect("/portal/portal/task_list.psml");
		} else if (value.equals(PortletsUtils.USER_MESSAGES_PAGE)) {
			response.sendRedirect("/portal/portal/message_list.psml?mode=1");
		}
	}
	
}
